package org.wattdepot.cli;

import java.io.IOException;
import org.wattdepot.client.WattDepotClientException;
//import org.wattdepot.client.ResourceNotFoundException;
//import org.wattdepot.client.MiscClientException;


/**
 * Use user's command to print out information extracted from WattDepot system.
 * @author wattdepot-cli-ewalu
 *
 */
public class Command {
  
  /** Constant string to eliminate redundant string literals. */
  public static final String exceptionMessage = "The WattDepot server encounters an error when " +
          "performing an operation. Check with admin for error." ;
  /** Constant string for argument in matching expression. */
  public static final String regExpression = ".* interval{1} .*";
  
  /**
   * Operate on the arguments and obtain data from WattDepot.
   * @param extract Data in WattDepot system
   * @param argument User's command argument
   */
  public static void obtainData(ExtractionInterface extract, StringInterface argument) {    
    try { 
      /**Command 2.2 End command line interface.*/
      if (("quit").compareTo(argument.parse()) == 0) {
        System.exit(0);
      }
      /**Command 2.1 list the commands to be used.*/
      else if (("help").compareTo(argument.parse()) == 0) {
        String usableCommands = extract.printHelp();
        System.out.println(usableCommands);
      }
      /**Command 2.3 Prints out information of one public source.*/
      else if (argument.getInput().startsWith("summary ")) {
        String desiredSource = argument.parse();
        String summary = "SubSources: " + extract.getSubSource(desiredSource);
        summary = summary.substring(0, summary.length() - 1);
        summary = summary.concat(extract.getDetail(desiredSource));
        summary = summary.concat(extract.getPerson(desiredSource));
        summary = summary.concat(extract.getPlace(desiredSource));
        summary = summary.concat(extract.getPlaceCoordinates(desiredSource));
        summary = summary.concat(extract.getAttribute(desiredSource));
        summary = summary.substring(0, summary.length() - 2);
        summary = summary.concat(extract.getData(desiredSource));
        System.out.println(summary);
      }
      /**Command 2.5 obtain information on a single sensor instance.*/
      else if (argument.getInput().startsWith("sensordata ") && 
        argument.getInput().matches(".* timestamp{1} .*")) {
        String holder[] = argument.parse().split(" ");
        String sensor = extract.getSensorInfo(holder[0], extract.getTime(holder[1]));
        System.out.println(sensor);
      }
      /**Command 2.7 to obtain power generated as specified by source and timestamp*/
      else if (argument.getInput().matches("^power{1} .*") &&
        argument.getInput().matches(".* timestamp{1} .*")) {
        String holder[] = argument.parse().split(" ");
        String power = extract.getPowerInfo(holder[0], holder[1], extract.getTime(holder[2]));
        System.out.println(power);
      }
      /* Command 2.4: List table of Sources */
      else if (argument.getInput().matches("^sources{1}$")) {
        StringBuffer output = extract.listSources();
        System.out.println(output);
      }
      /* Command 2.6: list sensordata by day */ 
      else if (argument.getInput().startsWith("dailysensordata ") &&
        argument.getInput().matches(".* day{1} .*")) {
        String holder[] = argument.parse().split(" ");
        StringBuffer output = extract.listSensorDataByDate(holder[0], holder[1]);
        System.out.println(output);
      }
      /* Command 2.8: display power in min, max, or average */
      else if (argument.getInput().startsWith("powerstats ") &&
        argument.getInput().matches(".* day{1} .*") && 
        argument.getInput().matches(regExpression) &&
        argument.getInput().matches(".* statistic{1} .*")) {
        String holder[] = argument.parse().split(" ");
        String output = extract.listPower(holder[0], holder[1], holder[2], 
            holder[3], holder[4]);
        System.out.println(output);
      }
      /* Command 2.9: display total amount of carbon or energy emitted. */
      else if (argument.getInput().startsWith("total ") &&
        argument.getInput().matches(".* day{1} .*") &&
        argument.getInput().matches(regExpression)) {
        String holder[] = argument.parse().split(" ");
        String output = extract.listTotal(holder[0], holder[1], holder[2], 
            holder[3], holder[4]);
        System.out.println(output);
      }
      /* Command 2.10: chart power into an html file showing graph */
      else if (argument.getInput().startsWith("chart power ") &&
        argument.getInput().matches(regExpression) &&
        argument.getInput().matches(".* file{1} .*")) {
        String holder[] = argument.parse().split(" ");
        String output = extract.chartPower(holder[0], holder[1], holder[2], 
            holder[3], holder[4], holder[5]);
        System.out.println(output);
      }
      /* Command 2.11: list the fuel types associated with source */
      else if (argument.getInput().startsWith("fueltypes ")) {
        String holder[] = argument.parse().split(" ");
        StringBuffer output = extract.getFueltype(holder[0]); 
        System.out.println(output);
      }
      /* Command 2.12: obtain total power of non-virtual sources */
      else if (argument.getInput().startsWith("totalpower ") &&
        (argument.getInput().matches(".* fueltype{1} .*"))) {
        String holder[] = argument.parse().split(" ");
        String output = extract.totalPower(holder[0], extract.getSubSource(holder[0]), 
          extract.getTime(holder[1]), holder[2]);
        System.out.println(output);
      }
      /* Command 2.13: Calculate lbs carbon /MW of carbon emitted */
      else if (argument.getInput().startsWith("carboncontent ") &&
        argument.getInput().matches(regExpression)) {
        String holder[] = argument.parse().split(" ");
        String output = extract.getCarboncontent(holder[0], holder[1], holder[2]); 
        System.out.println(output);
      }
      else {
        System.out.println("You inputted: " + argument.getInput() +
          "\nCheck your syntax. Type help.");
      }
    }
    catch (IOException anException) {
      System.out.println(exceptionMessage + "\n" + anException);
    }
    catch (WattDepotClientException anException) {
      System.out.println(exceptionMessage);
    }
    catch (StringIndexOutOfBoundsException outOfBoundException) {
      System.out.println("Type help for commands and check public source.");
    }
    catch (NumberFormatException anException) {
      System.out.println(anException);
    }
    catch (ArrayIndexOutOfBoundsException anException) {
      System.out.println("Type help for commands.");
    }
    catch (NullPointerException anException) {
      System.out.println("You inputted: " + argument.getInput() +
        "\nCheck your syntax. Type help.");
      System.out.println(anException);
    }
  }
}
